`
https://leetcode.cn/problems/binary-tree-pruning/
`

/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {TreeNode}
 */
var pruneTree = function (root) {
  const traverse = (root) => {
    if (root === null) return false

    const leftHasOne = traverse(root.left)
    const rightHasOne = traverse(root.right)

    // 左右子树中没有 1 的话，删掉结点
    if (!leftHasOne) root.left = null
    if (!rightHasOne) root.right = null

    return root.val === 1 || leftHasOne || rightHasOne
  }

  traverse(root)

  // 如果删到最后只剩一个值为 0 的根节点了，返回 null
  if (root.val === 0 && !root.left && !root.right) return null

  return root
};